package com.erdos.hyt.mall.config;

import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.Filter;
import java.io.*;
import java.util.Properties;

public class MallServiceInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

	public static String confile = "utils.properties";
	public static Properties properties = null;

	@Override
	protected Class<?>[] getRootConfigClasses() {
		return new Class[] { MallMvcConfig.class };
	}

	@Override
	protected String getServletName() {
		return "MallServiceServlet";
	}

	@Override
	protected Class<?>[] getServletConfigClasses() {
		properties = new Properties();
		InputStream inputStream = null;
		try {
			confile = MallServiceInitializer.class.getClassLoader().getResource("").getPath() + confile;
			File file = new File(confile);
			inputStream = new BufferedInputStream(new FileInputStream(file));
			properties.load(inputStream);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (inputStream != null) {
					inputStream.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	@Override
	protected String[] getServletMappings() {
		return new String[] { "/" };
	}
	protected Filter[] getServletFilters() {
		CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
		characterEncodingFilter.setEncoding("UTF-8");
		characterEncodingFilter.setForceEncoding(true);
		return new Filter[] { characterEncodingFilter };
	}

}
